//
//  ViewController.m
//  NetWork
//
//  Created by cocoazxj@163.com on 15/4/27.
//  Copyright (c) 2015年 handcool. All rights reserved.
//

#import "ViewController.h"
#import "XMLViewController.h"

@interface ViewController ()<NSURLConnectionDataDelegate>
@property (nonatomic, strong) NSMutableData *receiveData;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSMutableDictionary *mDict = [NSMutableDictionary dictionary];
    [mDict setValue:@"123" forKey:@"username"];
    [mDict setValue:@"123" forKey:@"pwd"];
    [self getData:mDict];
    [self getTable:mDict];
    [self postTable:mDict];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)postTable:(NSMutableDictionary *)mDict
{
    NSString *uString = [NSMutableString stringWithFormat:@"http://192.168.0.77:8080/MJServer/login"];
    NSURL *url = [NSURL URLWithString:uString];
    NSMutableURLRequest *requset = [NSMutableURLRequest requestWithURL:url];
    requset.HTTPMethod = @"POST";
    NSString *para = [NSString stringWithFormat:@"username=%@&pwd=%@",mDict[@"username"],mDict[@"pwd"]];
    requset.HTTPBody = [para dataUsingEncoding:NSUTF8StringEncoding];
    
    [NSURLConnection sendAsynchronousRequest:requset queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse* response, NSData* data, NSError* connectionError){
        if(data)
        {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            NSLog(@"--postTable-------------%@",dict[@"success"]);
        }
        else
        {
            
        }
    }];
}

#pragma mark -
#pragma mark sendAsynchronousRequest
- (void)getTable:(NSMutableDictionary *)mDict
{
    NSString *uString = [NSMutableString stringWithFormat:@"http://192.168.0.77:8080/MJServer/login?username=%@&pwd=%@",mDict[@"username"],mDict[@"pwd"]];
    NSURL *u = [NSURL URLWithString:uString];
    NSMutableURLRequest *requset = [NSMutableURLRequest requestWithURL:u];
    [NSURLConnection sendAsynchronousRequest:requset queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse* response, NSData* data, NSError* connectionError){
        if(data)
        {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            NSLog(@"%@",dict[@"success"]);
        }
        else
        {
            
        }
    }];
}

#pragma mark -
#pragma mark connectionWithRequest
- (void)getData:(NSMutableDictionary *)mDict
{
    NSString *uString = [NSMutableString stringWithFormat:@"http://192.168.0.77:8080/MJServer/login?username=%@&pwd=%@",mDict[@"username"],mDict[@"pwd"]];
    NSURL *u = [NSURL URLWithString:uString];
    NSMutableURLRequest *requset = [NSMutableURLRequest requestWithURL:u];
    NSURLConnection *connect = [NSURLConnection connectionWithRequest:requset delegate:self];
    [connect start];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    self.receiveData = [NSMutableData data];
    NSLog(@"--------------didReceiveResponse");
}

// 正在接收数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"--------------didReceiveData");
    [self.receiveData appendData:data];
}

// 完成接收数据
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    
    NSLog(@"--------------connectionDidFinishLoading");
    if(self.receiveData)
    {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:self.receiveData options:NSJSONReadingMutableLeaves error:nil];
        NSLog(@"-----------ReceiveData-------%@",dict[@"success"]);
    }
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"--------------didFailWithError");
}

- (IBAction)XMLButtonClicked:(id)sender
{
    
}
@end
